<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/token.jsp"%>
<%!
	// 我发送的通知列表
	String list(Input input, Integer uid) {
		Integer page = 1; // 页码
		Integer hospital = input.getInt("hospital", 20); // 每一页条目
		Integer itemCount = input.getInt("item_count", 20); // 每一页条目
		String sinceID = input.getDesc("since_id", "0"); // 下拉刷新
		String maxID = input.getDesc("max_id", "0"); // 上拉加载
		String date = input.getDesc("date", "");
		String type = input.getDesc("type", "1"); // 通知分类, 事务通知2 通知患者3
		// 查看某天发的通知
		if (!date.equals("")) {
			date = " and concat(FROM_UNIXTIME(n.cTime/1000,'%Y-%m-%d'), '') = '" + date + "'";
		}
		String id = "";
		String order = "desc"; // 排序
		Message msg = new Message(0);
		if (!sinceID.equals("0")) {
			id = " and n.id > " + sinceID;
			order = "asc";
		} 
		if (!maxID.equals("0")) {
			id = " and n.id < " + maxID;
		}
		String hql = "select new map(n.id as id, n.title as title, n.content as content, concat(FROM_UNIXTIME(n.cTime/1000,'%Y-%m-%d %H:%i:%s'), '')  as create_time, case when n.imgs is null then '[]' else imgs end as imgs, case when n.voices is null then '[]' else voices end as voices,n.feedback as feedback) from Notification as n where n.sender='" + uid + "' and n.status=" + STATUS_ENABLE + " and n.hospital=" + hospital + " and n.type=" + type + id + " order by n.id " + order;
		List<Map> notifictionList = QBuilder.db("basedb").queryHql(hql, itemCount, page);
		if (isEmpty(notifictionList)) {
			msg.setErrcodeAndErrmsg(30041, "没有更多通知");
			return msg.toJson();
		}
		hql = "select new map(nid as nid, count(isConfirmed) as state_count,isConfirmed as is_confirmed) from NotificationConfirm where hospital= " + hospital + " and status=" + STATUS_ENABLE + " group by nid, isConfirmed ";
		List<Map> notifictionConfirmList = QBuilder.db("basedb").queryHql(hql);
		Map<Integer, Long> isConfirmedMap = new HashMap<Integer, Long>();
		Map<Integer, Long> noConfirmedMap = new HashMap<Integer, Long>();
		for (Map map:notifictionConfirmList) {
			if ((Integer)map.get("is_confirmed") != 0) 
				isConfirmedMap.put((Integer)map.get("nid"), (Long)map.get("state_count"));
			else 
				noConfirmedMap.put((Integer)map.get("nid"), (Long)map.get("state_count"));
		}
		for (Map map:notifictionList) {
			Long isConfirmed = isConfirmedMap.get((Integer)map.get("id")) == null?0:isConfirmedMap.get((Integer)map.get("id"));
			Long noConfirmed = noConfirmedMap.get((Integer)map.get("id")) == null?0:noConfirmedMap.get((Integer)map.get("id"));
			map.put("receiver_total", isConfirmed + noConfirmed );
			map.put("receiver_is_confirmed", isConfirmed);
			map.put("receiver_no_confirmed",noConfirmed );
		}	
		if (order.equals("asc")) { 
			Collections.reverse(notifictionList);
		}
		msg.put("data", notifictionList);
		return msg.toJson();
	}
%>
<%
	Tools.encryptPrint(out, list(input, token.getUid()));
%>
